package com.leetcode.partition4;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author `RKC`
 * @date 2022/4/6 22:07
 */
public class LC310最小高度树 {

    private static final int N = 20010;
    private static int[] h = new int[N], e = new int[2 * N], ne = new int[2 * N];
    private static int[] down1 = new int[N], down2 = new int[N], up = new int[N], path = new int[N];
    private static int n, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(reader.readLine());
        int[][] edges = new int[n - 1][2];
        for (int i = 0; i < n - 1; i++) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            edges[i] = new int[]{a, b};
        }
        writer.write(findMinHeightTrees(n, edges).toString());
        writer.flush();
    }

    public static List<Integer> findMinHeightTrees(int _n, int[][] edges) {
        n = _n;
        Arrays.fill(h, -1);
        for (int[] edge : edges) {
            add(edge[0], edge[1]);
            add(edge[1], edge[0]);
        }
        dfsdown(0, -1);
        dfsup(0, -1);
        List<Integer> ans = new ArrayList<>();
        int minh = n;
        for (int i = 0; i < n; i++) minh = Math.min(minh, Math.max(up[i], down1[i]));
        for (int i = 0; i < n; i++) {
            if (Math.max(up[i], down1[i]) == minh) {
                ans.add(i);
            }
        }
        return ans;
    }

    private static void dfsdown(int u, int fa) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == fa) continue;
            dfsdown(v, u);
            if (down1[v] + 1 > down1[u]) {
                down2[u] = down1[u];
                down1[u] = down1[v] + 1;
                path[u] = v;
            } else if (down1[v] + 1 > down2[u]) {
                down2[u] = down1[v] + 1;
            }
        }
    }

    private static void dfsup(int u, int fa) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == fa) continue;
            if (path[u] == v) up[v] = Math.max(up[v], down2[u] + 1);
            else up[v] = Math.max(up[v], down1[u] + 1);
            up[v] = Math.max(up[v], up[u] + 1);
            dfsup(v, u);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
